<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
	自定义控制器
*/
class MY_Controller extends CI_Controller{
	
	protected $_config = array();
	protected $_url = array();
	
	public function __construct(){
		parent::__construct();
		$this->config->load('site', true);
		$this->_config = $this->config->item('site');
		$this->init_url();
	}
	
	public function init_url($root = ''){
		$url_root = rtrim($root ? $root : $this->_config['url_root'], '/');
		!$url_root && $url_root = '/';
		$url_str = $this->uri->uri_string;
		$url_dir = $this->router->directory;
		$i = 1;
		if($url_dir){
			$dirs = explode('/', trim($url_dir, '/'));
			$url_home = $dirs[0];
			$i = count($dirs) + 1;
			$url_base = $url_dir.$this->uri->segments[$i];
		}else{
			$url_base = $this->uri->segments[$i];
			$url_home = '/';
		}
		$method = $this->uri->segments[$i + 1];
		$url_method = $method ? trim($url_base.'/'.$method, '/') : '';
		$url_root = rtrim($url_root, '/').'/';
		$this->_url = [
			'dirname' => '/'.rtrim($url_dir, '/'),
			'root' => $url_root,
			'home' => rtrim($url_root.$url_home, '/'),
			'base' => rtrim($url_root.$url_base, '/'),
			'method' => rtrim($url_root.$url_method, '/'),
			'path' => rtrim($url_root.$url_str, '/')
		];
	}
	
	// 初始化smarty模板引擎
	public function init_smarty($params = array()){
		if(!$this->load->is_loaded('ci_smarty')){
			$this->load->library('ci_smarty', $params);
		}
		$assign = array(
			'config' => $this->_config,
			'tpl_root' => SMARTY_TPL
		);
		$this->ci_smarty->assign($assign);
	}
	
	// smarty懒加载
	public function load_smarty($params = array()){
		if($params['url_root']){
			$this->init_url($params['url_root']);
		}
		// 非ajax请求，开始模板渲染
		if(! $this->input->is_ajax_request()){
			$this->init_smarty();
			if($params['tpl_dir']){
				$this->ci_smarty->dir_extend($params['tpl_dir']);
			}
			$assign = [
				'tpl_dir' => $this->ci_smarty->getTemplateDir(0),
				'url' => $this->_url
			];
			if($params['assign']){
				$assign = array_merge($assign, $params['assign']);
			}
			$this->ci_smarty->assign($assign);
		}
	}
	
	// smarty 变量注册
	public function smarty_assign($assign){
		if($this->load->is_loaded('ci_smarty')){
			$this->ci_smarty->assign($assign);
		}
	}
	
	public function output_json($data = '参数未知错误', $code = 500){
		if(is_array($data)){
			$data['retcode'] = isset($data['retcode']) ? (int)$data['retcode'] : $code;
			$result = $data;
		}else{
			$result = array(
				'retcode' => (int)$code,
				'message' => $data
			);
		}
		$callback = $this->input->get_post('callback');
		if($callback){
			echo "$callback(".json_encode($result).")";
		}else{
			echo json_encode($result);
		}
		exit;
	}
	
}

/* 
	基础控制器
*/
class Base_Controller extends MY_Controller{
	
	public function __construct(){
		parent::__construct();
		$_router =& load_class('Router', 'core');
		$dirpath = $_router->fetch_directory();
		$this->load_smarty([
			'tpl_dir' => $dirpath
		]);
	}
	
	public function show_message($message = '', $code = 500, $redirect = '/'){
		if($this->input->is_ajax_request()){
			$this->output_json($message ? $message : '参数未知错误', $code);
		}else{
			if($code == 301){
				redirect($redirect);
				exit;
			}
			$this->ci_smarty->assign([
				'message' => $message ? $message : '页面不存在',
				'code' => $code
			]);
			$this->ci_smarty->display('tpl_message.htm');
		}
		exit;
	}
	
}